package cn.zlj.pethome.order.service.impl;

import cn.zlj.pethome.basic.config.BussinessException;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.dto.PayDto;
import cn.zlj.pethome.basic.util.LoginContext;
import cn.zlj.pethome.order.domain.OrderAuditLog;
import cn.zlj.pethome.order.domain.OrderPetAcquisition;
import cn.zlj.pethome.order.mapper.OrderAuditLogMapper;
import cn.zlj.pethome.order.service.IOrderPetAcquisitionService;
import cn.zlj.pethome.basic.service.impl.BaseServiceImpl;
import cn.zlj.pethome.org.domain.TEmployee;
import cn.zlj.pethome.org.mapper.EmployeeMapper;
import cn.zlj.pethome.pet.domain.Pet;
import cn.zlj.pethome.pet.domain.SearchMasterMsg;
import cn.zlj.pethome.pet.mapper.PetMapper;
import cn.zlj.pethome.pet.mapper.SearchMasterMsgMapper;
import cn.zlj.pethome.pet.service.IPetService;
import cn.zlj.pethome.pet.service.impl.PetServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zlj
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private IPetService petService;
    @Autowired
    private OrderAuditLogMapper orderAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    @Transactional
    public void cancel(Long id,HttpServletRequest request) {
        //取消订单订单id修改为-1 然后在修改寻组消息id为1 shopid设置为null
        Long eid=null;
            if(request==null){
                 eid=333L;
            }else {
                Logininfo logininfo = LoginContext.getLogininfo(request);
                TEmployee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
                 eid= employee.getId();
            }

        OrderPetAcquisition or = super.findById(id);
        if(or==null){
            throw new BussinessException("该订单不存在");
        }
        or.setState(-1);
        super.update(or);
        SearchMasterMsg msg = searchMasterMsgMapper.findById(or.getSearchMasterMsgId());
        if(msg==null){
            throw new BussinessException("寻主消息不存在");
        }
        msg.setState(1);
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
           //保存 取消日志
        addLog(eid, or);
    }

    private void addLog(Long eid, OrderPetAcquisition or) {
        OrderAuditLog orderAuditLog = new OrderAuditLog();
        orderAuditLog.setOrderId(or.getId());
        orderAuditLog.setEmpId(eid);
        orderAuditLog.setNode("订单被取消");
        orderAuditLogMapper.add(orderAuditLog);
    }

    @Override
    @Transactional
    public void confirm(PayDto payDto) {
        //订单确认修改状态为1 修改寻主msg为完成状态3 保存宠物信息
        OrderPetAcquisition or = super.findById(payDto.getId());
        if(or==null){
            throw new BussinessException("该订单不存在");
        }
        or.setState(1);
        or.setPrice(new BigDecimal(payDto.getMoney()));
        super.update(or);
        SearchMasterMsg msg = searchMasterMsgMapper.findById(or.getSearchMasterMsgId());
        if(msg==null){
            throw new BussinessException("寻主消息不存在");
        }
        msg.setState(3);
        searchMasterMsgMapper.update(msg);
        //新增宠物信息
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setTypeId(1L);
        pet.setShopId(msg.getShopId());
        pet.setCostprice(msg.getPrice().toString());
        petService.add(pet);
    }
}
